package gui.panel;

import gui.MainWindow;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ListModel;

import listeners.DisconnectButtonListener;
import listeners.NewConversationListener;

import network.NetworkInterface;
import network.User;

/**
 * This is basically where every networking components are created and initialized
 * @author Ibi
 *
 */
public class HomePanel extends JPanel {

	private MainTabs tabs;
	private JButton btnDisconnect;
	private JList listConnectedUsers;
	private DefaultListModel listConnectedUsersModel;
	private NetworkInterface networkInterface;

	public HomePanel(MainTabs tabs, NetworkInterface networkInterface) {
		super(new BorderLayout());
		this.networkInterface = networkInterface;
		this.tabs = tabs;
		this.initComponents();
		this.addComponents();
		this.addListeners();
	}

	private void addListeners() {
		// TODO Auto-generated method stub
		this.btnDisconnect.addActionListener(new DisconnectButtonListener(this));
		this.listConnectedUsers.addMouseListener(new NewConversationListener(this.getTabs()));
	}

	private void initComponents() {
		// TODO Auto-generated method stub
		this.btnDisconnect = new JButton("Disconnect");
		this.listConnectedUsersModel = new DefaultListModel();
		this.listConnectedUsers = new JList(this.listConnectedUsersModel);

	}
	
	public void addConnectedUser(String nickname) {
		if (!this.listConnectedUsersModel.contains(nickname)) 
			this.listConnectedUsersModel.addElement(nickname);
	}
	
	public void removeConnectedUser(String nickname) {
		this.listConnectedUsersModel.removeElement(nickname);
	}

	private void addComponents() {
		// TODO Auto-generated method stub
		JPanel panUser = new JPanel(new BorderLayout());
		JPanel panUserInfo = new JPanel(new FlowLayout());
		JPanel panUserActions = new JPanel(new FlowLayout());
		JPanel panCommunications = new JPanel(new BorderLayout());
		JPanel panCommunicationsActions = new JPanel(new FlowLayout());
		
		panUserActions.add(btnDisconnect);
		panUserInfo.add(new JLabel("Welcome, " + this.networkInterface.parseUserName(this.networkInterface.getNickname())));
		panUser.add(panUserInfo, BorderLayout.CENTER);
		panUser.add(panUserActions, BorderLayout.SOUTH);
		
		panCommunications.add(panCommunicationsActions, BorderLayout.SOUTH);
		panCommunications.add(new JScrollPane(this.listConnectedUsers), BorderLayout.CENTER);

		this.add(panUser, BorderLayout.WEST);
		this.add(panCommunications, BorderLayout.CENTER);
		
	}

	public MainTabs getTabs() {
		return tabs;
	}

	public void setTabs(MainTabs tabs) {
		this.tabs = tabs;
	}

	public JButton getBtnDisconnect() {
		return btnDisconnect;
	}

	public void setBtnDisconnect(JButton btnDisconnect) {
		this.btnDisconnect = btnDisconnect;
	}

	public JList getListConnectedUsers() {
		return listConnectedUsers;
	}

	public void setListConnectedUsers(JList listConnectedUsers) {
		this.listConnectedUsers = listConnectedUsers;
	}

	public NetworkInterface getNetworkInterface() {
		return networkInterface;
	}

	public void setNetworkInterface(NetworkInterface networkInterface) {
		this.networkInterface = networkInterface;
	}

}
